package cn.sirun.com.fca.personal.center.presenter.impl;

import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.util.rx.SimpleObserver;

import cn.sirun.com.fca.account.model.base.CarInfoDetailResponse;
import cn.sirun.com.fca.personal.center.constant.PersonalCenterActionConst;
import cn.sirun.com.fca.personal.center.loader.CarManagerLoader;
import cn.sirun.com.fca.personal.center.model.bean.CarListResponse;
import cn.sirun.com.fca.personal.center.model.request.SubmitPlateNumberRequest;
import cn.sirun.com.fca.personal.center.model.request.SubmitUnbindCarRequest;
import cn.sirun.com.fca.personal.center.presenter.ICarManagerPresenter;
import cn.sirun.com.fca.personal.center.presenter.PersonalCenterFlowPresenter;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.subjects.BehaviorSubject;

public class CarManagerPresenter extends PersonalCenterFlowPresenter<CarManagerLoader>
        implements ICarManagerPresenter {
    private BehaviorSubject<BaseResponse> mBehaviorSubject = BehaviorSubject.create();

    @Override
    protected CarManagerLoader createLoader() {
        return new CarManagerLoader();
    }

    @Override
    public void getCarList() {
        getLoader().getCarList()
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<CarListResponse>>() {

                    @Override
                    public void onNext(BaseResponse<CarListResponse> carResponse) {
                        super.onNext(carResponse);
                        getActionListener().onAction
                                (PersonalCenterActionConst.ACTION_GET_CAR_LIST_RESPONSE,
                                        carResponse);
                    }
                });
    }

    @Override
    public void setCurrentVehicle(String din) {
        getLoader().setCurrentVehicle(din)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<Void>>() {

                    @Override
                    public void onNext(BaseResponse<Void> response) {
                        super.onNext(response);
                        getActionListener().onAction
                                (PersonalCenterActionConst.ACTION_SET_CUR_CAR_RESPONSE,
                                        response);
                    }
                });
    }

    @Override
    public void editCarNumber(SubmitPlateNumberRequest request) {
        getLoader().editCarNumber(request)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<Void>>() {

                    @Override
                    public void onNext(BaseResponse<Void> response) {
                        super.onNext(response);
                        getActionListener().onAction
                                (PersonalCenterActionConst.ACTION_EDIT_PLATE_NUMBER_RESPONSE,
                                        response);
                    }
                });
    }

    @Override
    public void unbindCar(SubmitUnbindCarRequest request) {
        getLoader().unbindCar(request)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<Void>>() {

                    @Override
                    public void onNext(BaseResponse<Void> response) {
                        super.onNext(response);
                        getActionListener().onAction
                                (PersonalCenterActionConst.ACTION_UNBIND_CAR_RESPONSE,
                                        response);
                    }
                });
    }

    @Override
    public void getCarInfo(String din) {
        getLoader().getCarInfo(din)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<CarInfoDetailResponse>>() {

                    @Override
                    public void onNext(BaseResponse<CarInfoDetailResponse> carInfoResponse) {
                        super.onNext(carInfoResponse);
                        getActionListener().onAction
                                (PersonalCenterActionConst.ACTION_GET_CAR_INFO_RESPONSE,
                                        carInfoResponse);
                    }
                });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mBehaviorSubject.onComplete();
    }

}
